//
//  ZpSearchView.m
//  WeiBo_OC
//
//  Created by MacBook Pro on 16/5/24.
//  Copyright © 2016年 MacBook Pro. All rights reserved.
//

#import "ZpSearchView.h"

@interface ZpSearchView ()
@property (weak, nonatomic) IBOutlet UITextField *searchContentTextField;

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *searchTxtRightConstrain;

@end

@implementation ZpSearchView

- (instancetype)init{
    
    if (self = [super init]) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"ZpSearchView" owner:nil options:nil] lastObject];
    }
    return self;
}

- (void)awakeFromNib{
    self.width = ScreenWidth;
    [self setViewAttributes];
}

- (void)setViewAttributes{


    // 设置searchContentTextField左视图，并且必须设置leftViewMode属性，默认左视图不显示
    UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
    leftView.width = 40;
    leftView.height = 24;
    // 设置视图中内容的填充模式
    leftView.contentMode = UIViewContentModeScaleAspectFit;
    self.searchContentTextField.leftView = leftView;
    self.searchContentTextField.leftViewMode = UITextFieldViewModeAlways;
    
    // 将searchContentTextField视图放在父视图中所有子视图的最上方
    [self bringSubviewToFront:self.searchContentTextField];
    // 将取消按钮放在父视图中所有子视图的最下方
//    [self sendSubviewToBack:self.cancelBtn];
    
    [self.searchContentTextField addTarget:self action:@selector(searchInformation) forControlEvents:UIControlEventEditingDidBegin];
    [self.cancelBtn addTarget:self action:@selector(cancleSearch) forControlEvents:UIControlEventTouchUpInside];
}

- (void)searchInformation{
#warning 啊啊啊，为啥错误
//    self.searchTxtRightConstrain.constant = self.cancelBtn.width + 8;
     self.searchTxtRightConstrain.constant = self.cancelBtn.bounds.size.width + 8;
    [UIView animateWithDuration:0.5 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)cancleSearch{
    self.searchTxtRightConstrain.constant = 0;
    [self endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^{
        [self layoutIfNeeded];
    }];
}

@end
